package com.example.tests;

import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

public class TestBase {

	private StringBuffer verificationErrors = new StringBuffer();
	private static WebDriver driver;

	@BeforeTest
	public void setUp() throws Exception {
		driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
	}
	
	@AfterTest
	public void tearDown() throws Exception {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	protected void createGroup(GroupObject groupObject) {
		openMainPage();
		returnToGroupListPage();
		initGroupCreation();
		fillGroupForm(groupObject);
		submitGroupCreation();
		returnToGroupListPage();
	}

	private void submitGroupCreation() {
		driver.findElement(By.name("submit")).click();
	}

	private void fillGroupForm(GroupObject groupObject) {
		type("group_name", groupObject.name);
		type("group_header", groupObject.header);
		type("group_footer", groupObject.footer);
	}

	private void type(String locator, String name) {
		driver.findElement(By.name(locator)).clear();
		driver.findElement(By.name(locator)).sendKeys(name);
	}

	private void initGroupCreation() {
		driver.findElement(By.name("new")).click();
	}

	private void returnToGroupListPage() {
		driver.findElement(By.linkText("groups")).click();
	}

	public void openMainPage() {
		driver.get("http://localhost//addressbookv4.1.4/");
	}


	private boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}

	protected void returnToHomePage() {
		driver.findElement(By.linkText("home page")).click();
	}

	protected void submitContactCreation() {
		driver.findElement(By.name("submit")).click();
	}

	protected void fillContactForm(ContactObject contactObject) {
				type("firstname", contactObject.firstname);
				type("lastname", contactObject.lastname);
				type("address", contactObject.address);
				type("home", contactObject.home);
				type("mobile", contactObject.mobile);
				type("work", contactObject.work);
				type("email", contactObject.email);
				type("email2", contactObject.email2);
				select("bday", contactObject.birthday);
				select("bmonth", contactObject.birthmonth);
				type("byear", contactObject.birthyear);
				select("new_group", contactObject.group);				
				type("address2", contactObject.address2);
				type("phone2", contactObject.phone2);				
	}

	private void select(String locator, String name) {
		new Select(driver.findElement(By.name(locator))).selectByVisibleText(name);
	}

	protected void goToAddNewContactPage() {
		driver.findElement(By.linkText("add new")).click();
	}

	protected void createContact(ContactObject validContact) {
		openMainPage();
		goToAddNewContactPage();
		fillContactForm(validContact);
		submitContactCreation();
		returnToHomePage();
	}

}
